package com.ctsi.flowable.idm.remote;

import com.ctsi.flowable.idm.dto.GroupDTO;
import com.ctsi.organise.common.model.Person;
import org.flowable.idm.api.Group;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntity;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zhangjw
 * @Date: 2018/9/30 01:29
 * @Description:
 */
public abstract class FlowableIdmUtils {


    public static GroupEntity toGroupEntity(GroupDTO dto){
        GroupEntity group = new GroupEntityImpl();
        group.setName(dto.getPartyName());
        group.setType("assignment");
        group.setId(dto.getPartyId());
        group.setRevision(1);
        return group;
    }

    public static Group toGroupEntity1(GroupDTO dto){
        Group group = new GroupEntityImpl();
        group.setName(dto.getPartyName());
        group.setType("assignment");
        group.setId(dto.getPartyId());
        return group;
    }

    public static List<Group> toGroupEntity1(List<GroupDTO> dtos) {
        return dtos.stream().map(dto -> {return FlowableIdmUtils.toGroupEntity1(dto);}).collect(Collectors.toList());
    }

    public static List<GroupEntity> toGroupEntity(List<GroupDTO> dtos) {
        return dtos.stream().map(dto -> {return FlowableIdmUtils.toGroupEntity(dto);}).collect(Collectors.toList());
    }


    public static UserEntity toUserEntity(Person u){
        UserEntity user = new UserEntityImpl();
        user.setEmail(u.getEmail());
        user.setFirstName(u.getFirstName());
        user.setLastName(u.getLastName());
        user.setId(u.getPartyId());
        user.setTenantId(u.getTenementId());
        user.setRevision(1);
        return user;
    }

    public static List<org.flowable.idm.api.User> toUserEntity(List<Person> us){
        return us.stream().map(user -> {return FlowableIdmUtils.toUserEntity(user);}).collect(Collectors.toList());
    }

}
